package cn.org.eshow.webapp.action;

import cn.org.eshow.bean.query.ProductCategoryQuery;
import cn.org.eshow.common.page.Page;
import cn.org.eshow.model.ProductCategory;
import cn.org.eshow.service.ProductCategoryManager;
import cn.org.eshow.service.ProductManager;
import cn.org.eshow.util.JacksonUtil;
import cn.org.eshow.util.PageUtil;
import cn.org.eshow.webapp.action.response.ZtreeResponse;
import cn.org.eshow.webapp.util.RenderUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.convention.annotation.AllowedMethods;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Results({ @Result(name = "input", location = "add"),
		@Result(name = "list", type = "redirect", location = ""),
		@Result(name = "success", type = "redirect", location = "view/${id}"),
		@Result(name = "redirect", type = "redirect", location = "${redirect}") })
@AllowedMethods({"list","search","delete","view","update","save","addChild", "getAllCategory","deleteChild"})
public class ProductCategoryAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	@Autowired
	private ProductCategoryManager productCategoryManager;
	@Autowired
	private ProductManager productManager;

	private List<ProductCategory> productCategories;
	private ProductCategory productCategory;
	private ProductCategoryQuery query;

	private String jsonStr;


	/**
	 *
	 * @return
     */
	public String list() {
		productCategories = productCategoryManager.list(query);
		return LIST;
	}

	/**
	 *
	 * @return
     */
	public String search() {
		Page<ProductCategory> page = productCategoryManager.search(query);
		productCategories = page.getDataList();
		saveRequest("page", PageUtil.conversion(page));
		saveRequest("query",query);
		return LIST;
	}

	/**
	 *
	 */
	public void delete() {
		ProductCategory productCategory = productCategoryManager.get(id);
		if (productCategory != null) {
			this.deleteChild(productCategory);
			productCategory.setUpdateTime(new Date());
			productCategory.setEnabled(Boolean.FALSE);
			productCategory=productCategoryManager.save(productCategory);
			this.deleteChild(productCategory);

			RenderUtil.success("删除成功");
		} else {
			RenderUtil.failure("参数不正确");
		}
	}

	/**
	 *
	 * @return
     */
	public String view() {
		if (id != null) {
			productCategory = productCategoryManager.get(id);
		}
		return NONE;
	}

	/**
	 *
	 * @return
	 * @throws Exception
     */
	public String update() throws Exception {
		ProductCategory old = productCategoryManager.get(id);
		old.setName(productCategory.getName());
		old.setDescription(productCategory.getDescription());
		productCategoryManager.save(old);
		saveMessage("修改成功");
		return REDIRECT;
	}

	/**
	 * @return 添加节点
	 * @throws Exception
	 */
	public void addChild() throws Exception {
		if (id != null) {//添加非底层
			ProductCategory parentCategory = productCategoryManager.get(id);
			if (parentCategory == null) {
				return;
			}
//            productCategoryManager.addChild(parent,null);

			parentCategory.setUpdateTime(new Date());
			if (parentCategory.getChildrenSize() == null) {
				parentCategory.setChildrenSize(0);
			}
			parentCategory.setChildrenSize(parentCategory.getChildrenSize() + 1);
			parentCategory = productCategoryManager.save(parentCategory);
			ProductCategory child = new ProductCategory();
			child.setChildrenSize(0);
			if (!StringUtils.isEmpty(productCategory.getName())) {
				child.setName(productCategory.getName());
			} else {
				child.setName("新建节点");
			}
			child.setParent(parentCategory);
			child = productCategoryManager.save(child);

			RenderUtil.success("添加成功");
			return;
		} else {//添加最高层
//            productCategoryManager.addChild(null,null);
			ProductCategory child = new ProductCategory();
			child.setChildrenSize(0);
			if (!StringUtils.isEmpty(productCategory.getName())) {
				child.setName(productCategory.getName());
			} else {
				child.setName("新建节点");
			}
			child.setParent(null);
			child = productCategoryManager.save(child);

			RenderUtil.success("添加成功");
			return;
		}
	}

	/**
	 * 获取顶部分类集合
	 */
	public void getAllCategory() {
		List<ProductCategory> topProductCategories = productCategoryManager.list(new ProductCategoryQuery(Boolean.TRUE, Boolean.TRUE));//获取顶级分类
		List<ZtreeResponse> responses = new ArrayList<ZtreeResponse>();
//		ZtreeResponse rootResponse = new ZtreeResponse(1, 0, "全部", false);
//		responses.add(rootResponse);
		for (ProductCategory topCategory : topProductCategories) {
			ZtreeResponse response = new ZtreeResponse(topCategory, 0, true, true, false);
			response.setChildren(myChild(topCategory));
			responses.add(response);
		}
		jsonStr = JacksonUtil.toJson(responses);
		return;
	}


	/**
	 * 通过传入的分类,查询其子类。并添加到List中
	 */
	private List<ZtreeResponse> myChild(ProductCategory parentCategory) {
		List<ZtreeResponse> responses = new ArrayList<ZtreeResponse>();
		List<ProductCategory> childProductCategories = productCategoryManager.list(new ProductCategoryQuery(Boolean.TRUE, parentCategory.getId()));//获取指定子分类
		for (ProductCategory category : childProductCategories) {
			ZtreeResponse response = new ZtreeResponse(category, parentCategory.getId(), true, true, false);
			if (category.getChildrenSize() > 0) {
				response.setChildren(myChild(category));
			}
			responses.add(response);
		}
		return responses;
	}

	/**
	 * 删除子分类
	 */
	private void deleteChild(ProductCategory parentCategory) {
		List<ProductCategory> productCategories = productCategoryManager.list(new ProductCategoryQuery(Boolean.TRUE, parentCategory.getId()));//获取指定子分类
		for (ProductCategory category : productCategories) {
			category.setUpdateTime(new Date());
			category.setEnabled(false);
			category=productCategoryManager.save(category);
			if (category.getChildrenSize() > 0) {
				this.deleteChild(category);
			}
		}
	}

	public List<ProductCategory> getProductCategories() {
		return productCategories;
	}

	public void setProductCategories(List<ProductCategory> productCategories) {
		this.productCategories = productCategories;
	}

	public ProductCategory getProductCategory() {
		return productCategory;
	}

	public void setProductCategory(ProductCategory productCategory) {
		this.productCategory = productCategory;
	}

	public ProductCategoryQuery getQuery() {
		return query;
	}

	public void setQuery(ProductCategoryQuery query) {
		this.query = query;
	}

	public String getJsonStr() {
		return jsonStr;
	}

	public void setJsonStr(String jsonStr) {
		this.jsonStr = jsonStr;
	}
}